package com.zhj.jvm.test.ref;

import java.lang.ref.PhantomReference;
import java.lang.ref.ReferenceQueue;

/**
 * @author zhj
 */
class MyObject {
    @Override
    protected void finalize() throws Throwable {
        super.finalize();
        System.out.println("我要被回收了！！！");
    }
}
public class PhantomReferenceDemo {
    public static void main(String[] args) {
        MyObject object = new MyObject();
        ReferenceQueue queue = new ReferenceQueue();
        PhantomReference<MyObject> phantomReference = new PhantomReference<>(object, queue);
        // 垃圾回收
        object = null;
        System.gc();
        System.out.println("GC执行");
        // GC-虚引用-入队-出队 -> obj
        System.out.println(queue.poll());
    }
}
